package com.changgou.controller;

import com.changgou.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * @ProjectName: changgou_parent
 * @Package: com.changgou.controller
 * @ClassName: SearchController
 * @Author: 孙明田
 * @Description: 搜索控制类
 * @Date: 2020/6/19 19:55
 * @Version: 1.0
 */

@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping
    public Map search(@RequestParam Map<String, String> searchMap) {
        //处理特殊字符
        this.handleSearchMap(searchMap);
        Map searchResult = searchService.search(searchMap);
        return searchResult;
    }

    private void handleSearchMap(Map<String, String> searchMap) {
        //获取所有的键值对
        Set<Map.Entry<String, String>> entries = searchMap.entrySet();
        //遍历
        for (Map.Entry<String, String> entry : entries) {
            if (entry.getKey().startsWith("spec_")) {

                //将value值替换
                searchMap.put(entry.getKey(), entry.getValue().replace("+", "%2B"));
            }
        }

    }

}
